package com.cx.thread;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * @Author:cxy
 * @Date:2022/3/10 21:42
 */
public class MyThread extends Thread{

    public MyThread() {
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getUncaughtExceptionHandler());
        throw new RuntimeException();
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Thread.setDefaultUncaughtExceptionHandler(new MyExcHandler());
        final MyThread thread = new MyThread();
        thread.setUncaughtExceptionHandler(new MyExcHandler());
        thread.start();
        TimeUnit.SECONDS.sleep(1);
    }



    static class MyExcHandler implements Thread.UncaughtExceptionHandler{
        @Override
        public void uncaughtException(Thread t, Throwable e) {
            System.out.println("当前异常被我捕获:"+e);
        }
    }
}
